package com.api.gbhg.xxd.dao;

import cn.hutool.core.util.StrUtil;
import com.api.gbhg.daizhishun.util.ReadProperties;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.luoao.utils.information;
import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;

import java.util.*;

public class ReimbursementDao {

    private static final Logger logger = LoggerFactory.getLogger(ReimbursementDao.class);


    //查询表名
    public static String tablefname(String requestId){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select  l.tablename,s.id  from workflow_requestbase r,workflow_base s,workflow_bill l where\n" +
                "r.workflowid = s.id and s.formid = l.id and  r.requestid ="+requestId);
        rs.next();
        return  rs.getStringNoTrim("tablename");
    }

    //根据requestid查询该流程数据
    public static Map<String,Object> workflowinfo(String requestid, String requestname,String startMoney,String endMoney){


        ReadProperties properties = new ReadProperties();
        List<String> workflowid = properties.getworkflowid();

        //主表名
        String tablename = tablefname(requestid);

        //根据不同的表更改字段名
        String fieldname = "jbr";
        if (tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(1)))){
            fieldname = "jbry";
        }

        String money = "sqbxjehj";
        if (tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(3)))){
            money = "jlje";
        } else if (tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(2)))){
            money = "bxwbjehj";
        }else {
            money = "sqbxjehj";
        }

        String yslyname = "ysly";//预算来源
        String bmjjzcflname = "bmjjzcfl";//部门经济分类
        if (tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(4)))){
            bmjjzcflname = "bmysjjfl";
        }


        Map<String,Object> maps = new HashMap<>();
        //查询流程主表数据
        //jbrname
        RecordSet primaytable = com.api.gbhg.daizhishun.dao.ReimbursementDao.getPrimaytable(
                tablename,requestid,fieldname,money);
        //查询主表数据
        while (primaytable.next()){
            String sqbxjehj = primaytable.getString(money);
            System.err.println(money+"...."+sqbxjehj);
            double bxje = primaytable.getDouble(money);
            if(!startMoney.equals("") && !endMoney.equals("")){
                double sm = Double.parseDouble(startMoney);
                double em = Double.parseDouble(endMoney);
                if(bxje>=sm && bxje<=em){
                    //System.err.println("符合金额查询："+bxje+">"+sm+">"+em);
                }else{
                    //System.err.println("不符合金额查询："+bxje+">"+sm+">"+em);
                    continue;
                }
            }else{
                //System.err.println("没有金额查询："+bxje);
            }
            maps.put("requestid",primaytable.getStringNoTrim("requestid"));
            maps.put("requestname",primaytable.getStringNoTrim("requestname"));
            maps.put("jbr",primaytable.getStringNoTrim("lastname"));//经办人
            maps.put("sqrq",primaytable.getStringNoTrim("sqrq"));//申请日期
            maps.put("bxhjshzt",primaytable.getString("bxhjshzt"));//审核状态
            maps.put("sqbxjehj",primaytable.getStringNoTrim(money));//贷方金额
            maps.put("lcbh",primaytable.getStringNoTrim("lcbh"));//流程编号
            maps.put("currentnodetype",primaytable.getStringNoTrim("currentnodetype"));

            //YS10 缉私举报奖金报销
            if (tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(1)))){


                List ysxm = new ArrayList();//预算项目
                List bmjjzcfl = new ArrayList();//部门经济支出分类
                List glh = new ArrayList();//关联号
                Set ysly = new HashSet();//预算来源
                String ly = primaytable.getStringNoTrim("ysly");
                if (StrUtil.isNotEmpty(ly)){
                    ysly.add(information.getselectname(requestid,"ysly",ly));
                }

                String xm = primaytable.getStringNoTrim("ysxm");
                if (StrUtil.isNotEmpty(xm)){
                    ysxm.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.getProjectname(xm));
                    glh.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.number(xm));
                }

                String fl=primaytable.getStringNoTrim(bmjjzcflname);
                if (StrUtil.isNotEmpty(fl)){
                    bmjjzcfl.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.getbmjjczfl(fl));
                }
                maps.put("ysly",  StringUtils.join(ysly.toArray(), ","));
                maps.put("ysxm", Joiner.on(",").join((Iterable<?>) ysxm));
                maps.put("bmjjzcfl",Joiner.on(",").join((Iterable<?>) bmjjzcfl));

                List skr = new ArrayList();//收款人
                skr.add(primaytable.getStringNoTrim("skr"));

                maps.put("skr",Joiner.on(",").join((Iterable<?>) skr));
            }

            // YS06 培训费报销
            if (tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(0)))){

                List ysxm = new ArrayList();//预算项目
                List bmjjzcfl = new ArrayList();//部门经济支出分类
                Set ysly = new HashSet();//预算来源
                List glh = new ArrayList();//关联号
                String ly = primaytable.getStringNoTrim("ysly");
                if (StrUtil.isNotEmpty(ly)){
                    ysly.add(information.getselectname(requestid,"ysly",ly));
                }

                String xm = primaytable.getStringNoTrim("ysxm");
                if (StrUtil.isNotEmpty(xm)){
                    ysxm.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.getProjectname(xm));
                    glh.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.number(xm));
                }

                String fl=primaytable.getStringNoTrim(bmjjzcflname);
                if (StrUtil.isNotEmpty(fl)){
                    bmjjzcfl.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.getbmjjczfl(fl));
                }



                maps.put("bmjjzcfl",Joiner.on(",").join((Iterable<?>) bmjjzcfl));
                maps.put("ysly", StringUtils.join(ysly.toArray(), ","));
                maps.put("ysxm", Joiner.on(",").join((Iterable<?>) ysxm));
                maps.put("glh",Joiner.on(",").join((Iterable<?>) glh));

            }

            //主表id
            String mainid = primaytable.getStringNoTrim("id");

            //不是流程 YS10 缉私举报奖金报销  进行查询
            if (!tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(1))) && !tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(0)))){

                //查询明细表1
                RecordSet Detail1 = com.api.gbhg.daizhishun.dao.ReimbursementDao.getDetail1(tablename, mainid);

                Set ysly = new HashSet();//预算来源
                List ysxm = new ArrayList();//预算项目
                List bmjjzcfl = new ArrayList();//部门经济支出分类
                List glh = new ArrayList();//关联号
                while (Detail1.next()){

                    String ly = Detail1.getStringNoTrim(yslyname);
                    if (StrUtil.isNotEmpty(ly)){
                        ysly.add(information.getselectname(requestid,yslyname,ly));
                    }

                    String xm = Detail1.getStringNoTrim("ysxm");
                    if (StrUtil.isNotEmpty(xm)){
                        ysxm.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.getProjectname(xm));
                        glh.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.number(xm));
                    }

                    String fl=Detail1.getStringNoTrim(bmjjzcflname);
                    if (StrUtil.isNotEmpty(fl)){
                        bmjjzcfl.add(com.api.gbhg.daizhishun.dao.ReimbursementDao.getbmjjczfl(fl));
                    }

                }

                maps.put("bmjjzcfl", Joiner.on(",").join((Iterable<?>) bmjjzcfl));
                maps.put("ysly", StringUtils.join(ysly.toArray(), ","));
                maps.put("ysxm", Joiner.on(",").join((Iterable<?>) ysxm));
                maps.put("glh",Joiner.on(",").join((Iterable<?>) glh));
            }

            //不是流程 YS10 缉私举报奖金报销 进行查询
            if (!tablename.equals(VersionFlowUtil.getFlowTableName(workflowid.get(1))) ) {

                //查询明细表2
                RecordSet detail2 = com.api.gbhg.daizhishun.dao.ReimbursementDao.getDetail2(tablename, mainid);

                List skr = new ArrayList();//收款人
                while (detail2.next()){
                    skr.add(detail2.getStringNoTrim("skr"));
                }
                maps.put("skr",Joiner.on(",").join((Iterable<?>) skr));

            }
        }
        return maps;
    }



    //过滤报销流程
    public static RecordSet filter(String requestid,String startTime,String  endTime,String dw,String year,String requestId,String jbrId)  {

        RecordSet rs = new RecordSet();
        if (StrUtil.isNotEmpty(requestid)) {
            ReadProperties properties = new ReadProperties();
            String workflowid = properties.read2();//所有报销流程workflowid

            StringBuffer sb = new StringBuffer();
            sb.append("select se.requestid,ce.lastname,se.createdate,se.workflowid,se.requestname from  " +
                    "workflow_requestbase se,hrmResource ce,workflow_nodebase wn " +
                    " where se.workflowid in(" + workflowid + ")  AND se.creater = ce.id AND se.currentnodeid = wn.id  ");  //AND wn.nodename = '05 流程归档'(前端已把归档数据参数传过来，这里可以不用过滤wn.nodename = '05 流程归档')

            sb.append(" and se.requestid in(" + requestid + ")");
            //sb.append(" and se.status in("+properties.Accountingnodename()+")");
            logger.info("会计待推送列表sql语句-------："+sb.toString());

            //条形码
            if (StrUtil.isNotEmpty(requestId)){
                sb.append(" and se.requestid = " + requestId);
            }

            //单位
            if (StrUtil.isNotEmpty(dw)) {
                sb.append(" and ce.subcompanyid1 =" + dw);
            }

            //年份
            if (StrUtil.isNotEmpty(year)) {
                sb.append(" and YEAR(se.createdate) =" + year);
            }

            //时间
            if (StrUtil.isNotEmpty(startTime)
                    && StrUtil.isNotEmpty(endTime)) {

                sb.append(" and se.createdate >='" + startTime +
                        "' and se.createdate<='" + endTime + "'");
            }

            //经办人
            if (StrUtil.isNotEmpty(jbrId)) {

                sb.append(" and ce.id =" + jbrId);
            }

            sb.append(" order by se.requestid desc ");
            rs.executeQuery(sb.toString());

            logger.info("sql打印："+sb);
        }

        return rs;
    }
}
